package com.ha.springcloud.controller;


import com.ha.springcloud.pojo.Department;
import com.ha.springcloud.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.util.List;

//实现页面跳转不能使用restcontroller
@Controller
public class EmployeeController {

    @Autowired
    private RestTemplate restTemplate;

    //    private static final String REST_URL_PREFIX="http://localhost:8001";
//这里的地址应该是eureka中注册服务的名字
    private static final String REST_URL_PREFIX="http://PROVIDER-EMPLOYEE";



    @RequestMapping("/queryEmployeeList")
    public List<Employee> queryemployeeList(){
        List<Employee> employeeList =restTemplate.getForObject(REST_URL_PREFIX+"/queryEmployeeList",List.class);
        return employeeList;
    }

    @RequestMapping("/emps")
    public String list(Model model){
        System.out.println("hi");
        //restTemplate讲date类型数据转换为了String类型
        List<Employee> employeeList =restTemplate.getForObject(REST_URL_PREFIX+"/queryEmployeeList",List.class);
        System.out.println(employeeList);
        model.addAttribute("emps",employeeList);
        return "emp/list";
    }

    @RequestMapping("/add")
    public String add(Model model){
        //查询出所有部门的信息
        List<Department> departments = restTemplate.getForObject(REST_URL_PREFIX+"/getDep",List.class);
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee){
        //添加的操作
        String id = restTemplate.getForObject(REST_URL_PREFIX+"/maxId",String.class);
//        restTemplate.postForObject(REST_URL_PREFIX+"/setId",id,employee,Employee.class);
        employee.setId(id);
        System.out.println(employee);
        restTemplate.postForObject(REST_URL_PREFIX+"/addEmp",employee,Integer.class);
        return "redirect:/emps";
    }

    @GetMapping("/update/{id}")
    public String updateEmp(@PathVariable("id")String id, Model model){
        Employee employee=restTemplate.getForObject(REST_URL_PREFIX+"/emp/get/"+id, Employee.class);
        model.addAttribute("emp",employee);
        List<Department> departments = restTemplate.getForObject(REST_URL_PREFIX+"/getDep",List.class);
        model.addAttribute("departments",departments);
        return "emp/update";
    }

    @PostMapping("/updateEmp")
    public String updateEmp(Employee employee){
        System.out.println(employee);
        //添加的操作
        restTemplate.postForObject(REST_URL_PREFIX+"/updateEmp",employee,Integer.class);
        return "redirect:/emps";
    }

    @GetMapping("/delete/{id}")
    public String deleteEmp(@PathVariable("id")String id){

        restTemplate.getForObject(REST_URL_PREFIX+"/delete/"+id, Integer.class);
        return "redirect:/emps";
    }

}
